package service;

import mapper.ConsoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import pojos.common.ReplyMessage;
import utils.Md5Utils;
import utils.SpecialCodeReplaceUtil;

/**
 * 管理员信息服务
 */
public class AdminInfoService {

    private final Logger logger = LoggerFactory.getLogger(AdminInfoService.class);
    private ConsoleMapper consoleMapper;

    @Autowired
    public AdminInfoService(ConsoleMapper consoleMapper) {
        this.consoleMapper = consoleMapper;
    }

    /**
     * 修改管理员用户名
     */
    public ReplyMessage updateLoginName(int adminId, String loginName){
        loginName = SpecialCodeReplaceUtil.doReplace(loginName);

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            consoleMapper.updateAdminLoginName(adminId, loginName);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改管理员登录名异常，管理员ID："+adminId+"，管理员登录名："+loginName);

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 修改管理员密码
     */
    public ReplyMessage updatePassword(int adminId, String password){
        password = Md5Utils.getMd5(password);

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            consoleMapper.updateAdminPassword(adminId, password);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改管理员密码异常，管理员ID："+adminId+"，密码："+password);

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 修改管理员手机号
     */
    public ReplyMessage updatePhone(int adminId, String phone){
        ReplyMessage replyMessage = new ReplyMessage();
        try {
            consoleMapper.updateAdminPhone(adminId, phone);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改管理员手机号异常，ID："+adminId+"，手机号："+phone);

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 修改管理员真实姓名
     */
    public ReplyMessage updateRealName(int adminId, String realName){
        String realNameRep = SpecialCodeReplaceUtil.doReplace(realName);

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            consoleMapper.updateAdminRealName(adminId,realNameRep);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改管理员真实姓名异常，管理员ID："+adminId+"，真实姓名："+realName);

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }
}
